1 00:00:00,380 --> 00:00:01,040 Hey there. 2 00:00:01,040 --> 00:00:01,950 Welcome back. 3 00:00:01,970 --> 00:00:05,930 Let's go ahead and continue to expand our knowledge of the Lua programming language. 4 00:00:05,930 --> 00:00:09,140 This lecture is going to cover loops today a loop. 5 00:00:09,140 --> 00:00:13,970 And programming is a block of code that is continually executed until a condition is met. 6 00:00:13,970 --> 00:00:19,120 Some loops may execute for infinity, and others may execute for a set number of times. 7 00:00:19,130 --> 00:00:25,490 In Lua there are three loops you'll want to note the for loop, the while loop, and the repeat loop. 8 00:00:25,520 --> 00:00:31,880 The for loop is when you want to execute a block of code for an exact number of times. 9 00:00:31,880 --> 00:00:38,480 However, we can also use a for loop using this function called Ipairs to loop through arrays or tables 10 00:00:38,480 --> 00:00:44,510 that have index value pairs, and then we can also use the pairs function to loop through dictionaries 11 00:00:44,510 --> 00:00:46,880 or tables that have key value pairs. 12 00:00:46,880 --> 00:00:53,420 You can see one represents I for index and one amidst the I to represent key value pairs. 13 00:00:53,750 --> 00:00:56,450 The next loop we're going to take a look at is the while loop. 14 00:00:56,450 --> 00:01:01,220 And the while loop will continually execute a block of code until a specific condition is met. 15 00:01:01,220 --> 00:01:03,890 Or we can have them execute for infinity. 16 00:01:03,890 --> 00:01:05,840 And lastly is the repeat loop. 17 00:01:05,840 --> 00:01:11,090 And the repeat loop also continually executes a block of code until a condition is met, or we can execute 18 00:01:11,090 --> 00:01:12,410 them for infinity. 19 00:01:12,440 --> 00:01:15,740 Now you might say, isn't that the exact same thing as a while loop? 20 00:01:15,740 --> 00:01:19,670 Well, they are pretty similar, but there is one small difference that we're going to take a look at 21 00:01:19,670 --> 00:01:20,450 in a moment. 22 00:01:20,690 --> 00:01:23,600 Let's first go ahead and take a look at the for loop. 23 00:01:23,600 --> 00:01:29,990 Now, as I said, the for loop is for when we want to execute a code, a set or finite number of times. 24 00:01:29,990 --> 00:01:36,440 To create a for loop, we first type the for keyword, and afterwards we get to create a variable. 25 00:01:36,440 --> 00:01:41,780 And this variable is going to help us keep track at what iteration we are in our for loop. 26 00:01:41,780 --> 00:01:47,660 Most of the time we call this variable I, which stands for index, and we start at at a value typically 27 00:01:47,660 --> 00:01:48,560 of one. 28 00:01:48,590 --> 00:01:50,300 Next we're going to put a comma. 29 00:01:50,300 --> 00:01:55,640 And after this we're going to put the value or the number of times we want this for loop to execute. 30 00:01:55,640 --> 00:01:58,880 So for example maybe we want the for loop to execute ten times. 31 00:01:58,880 --> 00:02:00,440 So I would put ten here. 32 00:02:00,650 --> 00:02:02,570 And then we can put another comma. 33 00:02:02,570 --> 00:02:08,450 And this represents the number that we should increment I by each time we loop. 34 00:02:08,450 --> 00:02:13,490 So if I put one here that means I want to increment I by one each time through this loop. 35 00:02:13,490 --> 00:02:16,160 And that means this loop will execute ten times. 36 00:02:16,160 --> 00:02:21,470 However, if I put a number like two here, that means every single time through this loop I is going 37 00:02:21,470 --> 00:02:26,210 to be incremented by two instead, which will cut down the number of times this loop is going to be 38 00:02:26,210 --> 00:02:26,990 executed. 39 00:02:27,110 --> 00:02:31,370 And we can also omit the last number completely and just have two numbers. 40 00:02:31,370 --> 00:02:35,600 And that means by default our I will be incremented by one. 41 00:02:35,600 --> 00:02:38,240 After we do this, we're going to put another keyword down. 42 00:02:38,240 --> 00:02:40,430 And this is called the do keyword. 43 00:02:40,430 --> 00:02:44,330 And what the do keyword does is it creates a new scope. 44 00:02:44,330 --> 00:02:50,030 So if I hit enter, as you can see an n keyword appeared here because now we have a new scope that belongs 45 00:02:50,030 --> 00:02:51,170 to this for loop. 46 00:02:51,440 --> 00:02:54,770 Inside of this for loop I'm going to do is use our print function. 47 00:02:54,770 --> 00:02:58,730 And I'm going to print out I or our index into the console. 48 00:02:58,760 --> 00:03:03,740 So we can go ahead and go to our test tab and hit run. 49 00:03:05,500 --> 00:03:11,710 And as you can see here inside of our console, I has been printed one two all the way down to ten. 50 00:03:11,710 --> 00:03:15,220 So this for loop executed exactly ten times. 51 00:03:15,220 --> 00:03:18,460 And you can also see how fast this for loop executed. 52 00:03:18,460 --> 00:03:26,110 Because each time we get to see the milliseconds here, it took it one millisecond to execute it ten 53 00:03:26,110 --> 00:03:26,470 times. 54 00:03:26,470 --> 00:03:28,900 So it's almost instantaneous right. 55 00:03:28,930 --> 00:03:31,420 For loops are extremely quick. 56 00:03:31,810 --> 00:03:38,290 Now if I decided to put that third value in there and instead increment I by two each time, and then 57 00:03:38,290 --> 00:03:41,230 we hit run and we go ahead and look at the output. 58 00:03:41,230 --> 00:03:43,900 As you can see we started with an index of one. 59 00:03:43,900 --> 00:03:46,780 We added two to our one and we got three. 60 00:03:46,780 --> 00:03:48,940 And we keep adding two each time. 61 00:03:48,940 --> 00:03:54,940 And that means our for loop only executed half as many times or five instead of ten. 62 00:03:56,040 --> 00:04:01,230 And this is because once I has exceeded the value of ten, it's no longer going to execute. 63 00:04:01,230 --> 00:04:05,160 It basically checks for that condition and then it breaks out of our for loop. 64 00:04:05,830 --> 00:04:10,240 The next for loop, we can go ahead and take a look at is the for I pairs loop. 65 00:04:10,240 --> 00:04:13,450 And again as I said this is for looping through arrays. 66 00:04:13,450 --> 00:04:15,580 So what I'm going to do is I'm going to create a variable. 67 00:04:15,580 --> 00:04:17,650 We can call it example array. 68 00:04:17,650 --> 00:04:19,690 And we're going to create a new table here. 69 00:04:19,690 --> 00:04:23,710 And inside of this table we can just insert a bunch of random numbers like 32. 70 00:04:23,740 --> 00:04:30,360 We can put in 95 -1,043.4 50 -12. 71 00:04:30,370 --> 00:04:30,760 Right. 72 00:04:30,760 --> 00:04:34,840 So here we have an array with a bunch of different values inside of it. 73 00:04:34,990 --> 00:04:38,110 And what we're going to do is put our for keyword down again. 74 00:04:38,110 --> 00:04:42,250 But this time we're going to need to create two variables instead of one. 75 00:04:42,250 --> 00:04:45,910 And this is because we need one variable to store the index. 76 00:04:45,910 --> 00:04:49,690 And we need another variable to store the value at that index. 77 00:04:49,690 --> 00:04:53,830 So typically most developers name the first variable I. 78 00:04:53,860 --> 00:04:56,080 And then we put a comma afterwards. 79 00:04:56,080 --> 00:05:00,370 And then the second variable we typically call it v to represent value. 80 00:05:00,400 --> 00:05:06,910 Afterwards we have to use another keyword in Lua and this is called n and after this we need to pass 81 00:05:06,910 --> 00:05:10,150 a function that's going to act as the iterator. 82 00:05:10,150 --> 00:05:15,850 And an iterating function is simply one that is going to loop through all of the values in our table 83 00:05:15,850 --> 00:05:18,400 and return them back to these variables. 84 00:05:18,400 --> 00:05:21,490 And that is going to be the I pairs function. 85 00:05:21,490 --> 00:05:26,170 So as you can see right here, it turns an iterator function and the table for use in a loop. 86 00:05:26,170 --> 00:05:29,170 So we're going to use the I pairs function. 87 00:05:29,170 --> 00:05:30,880 And we get to pass our table here. 88 00:05:30,880 --> 00:05:32,890 So we'll pass example array. 89 00:05:33,160 --> 00:05:37,390 And then afterward we need to put another do keyword to create a new scope. 90 00:05:37,690 --> 00:05:44,440 And now what we could go ahead and do is print out the index and the value through each iteration of 91 00:05:44,440 --> 00:05:44,890 this loop. 92 00:05:44,890 --> 00:05:48,190 So I'm going to put a string here and I'm just going to say index. 93 00:05:48,370 --> 00:05:51,370 And then we concatenate this with I. 94 00:05:51,550 --> 00:05:54,130 And then we'll concatenate it with another string. 95 00:05:54,130 --> 00:05:57,340 And this time we're going to say value put another colon. 96 00:05:57,340 --> 00:05:59,950 And then we can concatenate this with v. 97 00:06:01,020 --> 00:06:05,170 So now this should loop through every single index value pair inside of our array. 98 00:06:05,190 --> 00:06:09,720 So through the first iteration we should get I of one and an I of one. 99 00:06:09,720 --> 00:06:11,730 We should get the value of 32. 100 00:06:11,730 --> 00:06:14,590 At the index of two we'll get 95 and so on. 101 00:06:14,610 --> 00:06:16,410 So let's go ahead and hit run. 102 00:06:18,610 --> 00:06:19,600 As you can see, here we go. 103 00:06:19,600 --> 00:06:21,790 We get index one and our index one. 104 00:06:21,790 --> 00:06:23,440 And we have the value of 32. 105 00:06:23,440 --> 00:06:26,800 At index two we have the value of 95 and so on and so forth. 106 00:06:26,800 --> 00:06:30,550 And as you can see it executes through our array chronologically. 107 00:06:30,550 --> 00:06:35,740 It starts at one and continues going through our array in order. 108 00:06:35,740 --> 00:06:36,100 Right. 109 00:06:36,100 --> 00:06:38,740 Because arrays have an order to them. 110 00:06:38,740 --> 00:06:40,000 They're organized. 111 00:06:40,390 --> 00:06:44,710 However, we know that dictionaries on the other hand, are not organized. 112 00:06:44,710 --> 00:06:47,590 And that's where the for pairs loop comes into play. 113 00:06:47,980 --> 00:06:51,460 So we're going to go through the exact same process as we did up here. 114 00:06:51,460 --> 00:06:55,720 I'm going to create a new variable I'll just call it example dictionary. 115 00:06:56,310 --> 00:06:59,690 And inside of here, we're just going to make some random key value pairs. 116 00:06:59,700 --> 00:07:03,990 I can call this first key something like data one and set it equal to 50. 117 00:07:04,020 --> 00:07:05,100 I'll create another one. 118 00:07:05,100 --> 00:07:07,710 Data two set it equal to 100. 119 00:07:07,740 --> 00:07:08,610 Data three. 120 00:07:08,610 --> 00:07:12,840 We can set to like 150 and then data four we can set to 200. 121 00:07:12,960 --> 00:07:16,890 So here we have our dictionary with our keys and our values. 122 00:07:17,220 --> 00:07:20,850 And what we need to do next is put our four keyword down. 123 00:07:21,030 --> 00:07:27,180 And the only difference I'm going to do here is instead of naming the first variable I, I'm instead 124 00:07:27,180 --> 00:07:33,660 going to name it K to let myself know that, hey, we're going to be looping through a dictionary that 125 00:07:33,660 --> 00:07:35,340 has key value pairs. 126 00:07:35,340 --> 00:07:38,550 So we have k and v key and value. 127 00:07:38,550 --> 00:07:41,310 And then we put our in keyword again. 128 00:07:41,310 --> 00:07:44,670 And this time we're going to be using the pairs function. 129 00:07:44,670 --> 00:07:47,880 And we can go ahead and pass our dictionary here. 130 00:07:48,880 --> 00:07:52,690 Put our do keyword down again and hit enter to create a new scope. 131 00:07:52,780 --> 00:07:55,810 And now what I'm going to do here is use our print function again. 132 00:07:55,810 --> 00:08:03,040 And this time I want to print out the keys so we can concatenate this with K and then concatenate that 133 00:08:03,040 --> 00:08:04,120 with another string. 134 00:08:04,120 --> 00:08:05,380 I'll just say value. 135 00:08:05,380 --> 00:08:08,620 Put a colon here and then concatenate that with v. 136 00:08:08,920 --> 00:08:12,880 So now we should loop through all of the different key value pairs in this table. 137 00:08:12,880 --> 00:08:14,990 And it's going to be printed out into our console. 138 00:08:15,010 --> 00:08:19,690 However what you're going to see is it's not going to print them in order. 139 00:08:19,720 --> 00:08:23,740 We might print data to first or we might print data for first. 140 00:08:23,770 --> 00:08:25,270 It's not going to be an order. 141 00:08:25,270 --> 00:08:28,360 And that's because dictionaries do not have an order to them. 142 00:08:28,360 --> 00:08:29,920 They're not like arrays. 143 00:08:29,920 --> 00:08:31,900 So if we go ahead and hit run here. 144 00:08:33,910 --> 00:08:39,760 As you can see, order was not maintained when we were looping through that dictionary. 145 00:08:39,790 --> 00:08:44,920 Data one got printed first with a value of 50, but then all of a sudden data three got printed next 146 00:08:44,920 --> 00:08:46,270 with a value of 150. 147 00:08:46,300 --> 00:08:51,280 Then data two with a value of 100 and then data four with a value of 200. 148 00:08:51,310 --> 00:08:59,740 So as you can see with our array, order was maintained but with our dictionary order was not maintained. 149 00:09:00,980 --> 00:09:01,340 Okay. 150 00:09:01,340 --> 00:09:05,450 So let's go ahead and take a look at the next loop here, which is our while loop. 151 00:09:05,450 --> 00:09:08,540 And these loops execute repeatedly until a condition is met. 152 00:09:08,570 --> 00:09:15,440 However, it's very crucial to note that these loops can be dangerous as they have the ability to crash 153 00:09:15,440 --> 00:09:20,390 our game if we do not yield the loop when it continually executes forever. 154 00:09:20,420 --> 00:09:22,640 What do I mean by yielding the loop? 155 00:09:22,670 --> 00:09:28,760 Well, when the main thread of execution hops into the block of code or the scope for that loop and 156 00:09:28,760 --> 00:09:35,030 starts executing, it needs to be able to yield or escape out of the loop at some point in order to 157 00:09:35,030 --> 00:09:41,030 execute other scripts in your game and other crucial engine tasks to keep the game running smoothly. 158 00:09:41,060 --> 00:09:46,730 When you have an infinite loop and it does not yield, it holds the main thread of execution hostage, 159 00:09:46,730 --> 00:09:52,730 and that thread will not be able to execute any other processes in the game engine which will cause 160 00:09:52,730 --> 00:09:54,710 the game to freeze and crash. 161 00:09:55,040 --> 00:10:00,800 The four loops that we took a look at here earlier do not require a yield, and that's because they 162 00:10:00,800 --> 00:10:01,770 are finite, right? 163 00:10:01,790 --> 00:10:04,460 They only execute a finite number of times. 164 00:10:04,460 --> 00:10:12,050 Even if you have a for loop that executes a thousand times, those executions will happen almost instantaneously. 165 00:10:12,050 --> 00:10:14,210 So a yield is not needed. 166 00:10:14,210 --> 00:10:20,240 But if we have a while loop that executes forever, we must yield the main thread of execution to avoid 167 00:10:20,240 --> 00:10:21,070 a crash. 168 00:10:21,080 --> 00:10:26,450 So I'm going to demonstrate to you what happens when we do not yield a while loop. 169 00:10:26,480 --> 00:10:33,230 To create a while loop, we type out the while keyword and after this while keyword, we need to create 170 00:10:33,230 --> 00:10:34,590 a condition to check. 171 00:10:34,610 --> 00:10:36,860 So this could be any condition in your game. 172 00:10:36,860 --> 00:10:42,200 Maybe you want to check if the value of a specific property is equal to another value, or you want 173 00:10:42,200 --> 00:10:45,530 to check if a number is greater than or equal to another number, whatever. 174 00:10:45,530 --> 00:10:49,370 But in this case, I want to create an infinitely executing loop. 175 00:10:49,370 --> 00:10:52,490 So all I'm going to do is just pass the value of true here. 176 00:10:52,490 --> 00:10:55,370 And then we put do again to create a new scope. 177 00:10:55,370 --> 00:10:59,290 And now we have created an infinitely executing loop. 178 00:10:59,300 --> 00:11:04,490 So our interpreter or the main thread of execution is going to start at the top of our script. 179 00:11:04,490 --> 00:11:08,480 It's going to go down through each of these for loops and execute every single one of them. 180 00:11:08,480 --> 00:11:13,820 And then eventually it's going to hit this while loop here, and it's going to hop into this while loop. 181 00:11:13,820 --> 00:11:18,800 And if this while loop does not yield, that thread is going to be stuck in here forever, which is 182 00:11:18,800 --> 00:11:20,390 going to cause our game to crash. 183 00:11:20,390 --> 00:11:25,160 So I'm going to do in this while loop is just print out like high into the console. 184 00:11:25,160 --> 00:11:30,050 And when I hit run you're going to see studio freeze and probably crash. 185 00:11:30,050 --> 00:11:31,700 So let's go ahead and see what happens. 186 00:11:32,500 --> 00:11:33,460 We hit run. 187 00:11:34,250 --> 00:11:39,470 And as you can see already, our game is refusing to load and my mouse is lagging. 188 00:11:39,470 --> 00:11:40,210 And there we go. 189 00:11:40,220 --> 00:11:42,140 Studio has just crashed. 190 00:11:43,460 --> 00:11:48,770 Thankfully, studio has an inbuilt feature where if it detects that the main thread of execution is 191 00:11:48,770 --> 00:11:56,090 stuck in a forever loop, then it will create a script timeout error and it says exhausted allowed execution 192 00:11:56,090 --> 00:11:56,330 time. 193 00:11:56,330 --> 00:12:01,700 So it recognized that our thread got stuck in an infinite loop and eventually stopped the game entirely 194 00:12:01,700 --> 00:12:03,440 and allowed us to go back to our script. 195 00:12:03,440 --> 00:12:10,200 But as you can see, having an infinite loop that does not yield is bad news for our game. 196 00:12:10,230 --> 00:12:12,770 All right, completely bad news. 197 00:12:13,870 --> 00:12:19,750 So now that we know the dangers of using while loops, your next question is probably going to be how 198 00:12:19,750 --> 00:12:22,090 do we yield out of a while loop? 199 00:12:22,120 --> 00:12:28,870 Well, we can do so by using a function in another library called the Task library. 200 00:12:28,900 --> 00:12:35,650 The task library holds several functions to help us manage the yielding and execution of our code. 201 00:12:35,680 --> 00:12:39,430 There's one function in the task library called task Dot. 202 00:12:39,460 --> 00:12:40,020 Wait. 203 00:12:40,030 --> 00:12:44,830 So to access the task library, I simply type out task and then index it. 204 00:12:44,830 --> 00:12:46,990 And here we can see several different functions. 205 00:12:46,990 --> 00:12:49,870 But the one we're going to take a look at here is the wait function. 206 00:12:49,870 --> 00:12:55,930 And here's the description it says yields the current thread until the next heartbeat in which the given 207 00:12:55,930 --> 00:12:59,070 duration in seconds has passed without throttling. 208 00:12:59,080 --> 00:13:05,620 So what this is saying is we pass a number to this function, and this function is going to allow the 209 00:13:05,620 --> 00:13:08,860 thread of execution to exit out of whatever block of code it's in. 210 00:13:08,860 --> 00:13:14,740 And then after that duration is up, it's going to hop back into that block of code it originally left 211 00:13:14,740 --> 00:13:16,060 and continue executing. 212 00:13:16,360 --> 00:13:18,430 So we can autofill this here. 213 00:13:18,430 --> 00:13:23,170 And we can go ahead and pass a number to represent how many seconds we would like to yield for. 214 00:13:23,200 --> 00:13:30,070 So if I put like one here, what's going to happen is the main thread is going to print hi in the console. 215 00:13:30,070 --> 00:13:33,010 And then it's going to encounter this yield function here. 216 00:13:33,010 --> 00:13:37,360 It's going to hop out of this while loop and start executing other important game stuff. 217 00:13:37,360 --> 00:13:43,510 And then once one second has surpassed it hops back into the original point and continues the next cycle 218 00:13:43,510 --> 00:13:44,260 of the while loop. 219 00:13:44,260 --> 00:13:49,600 So it'll print hi again, and then it'll go and yield again and continue doing this on and on and on 220 00:13:49,600 --> 00:13:50,770 for infinity. 221 00:13:51,070 --> 00:13:57,520 Now, if you leave this blank here, what's going to happen is this while loop is going to execute extremely 222 00:13:57,520 --> 00:13:58,000 fast. 223 00:13:58,000 --> 00:14:06,370 And this is because the default time duration for tasks wait is 0.016 seconds, which happens to match 224 00:14:06,370 --> 00:14:08,290 60 frames per second. 225 00:14:08,320 --> 00:14:13,900 This is because Roblox runs at 60 frames per second, and the time duration between each of those frames 226 00:14:13,930 --> 00:14:16,780 is 0.016 seconds. 227 00:14:16,780 --> 00:14:20,320 But for this example, I'm going to put the value of one here. 228 00:14:20,320 --> 00:14:26,050 And then if we hit run, you're going to see inside of our console every single second we're getting 229 00:14:26,050 --> 00:14:27,690 a new high printed out. 230 00:14:27,700 --> 00:14:32,800 So one second goes by get another high another second high high high. 231 00:14:32,800 --> 00:14:35,740 And it's going to continually do this for infinity. 232 00:14:35,740 --> 00:14:37,930 That's the purpose of an infinite while loop. 233 00:14:37,930 --> 00:14:41,290 We have a block of code that we want to execute forever and ever. 234 00:14:42,100 --> 00:14:47,440 Now we can also have our while loop act like a for loop, and it's a little bit more complicated. 235 00:14:47,440 --> 00:14:51,220 But what I'm going to do is I'll create a variable outside of my while loop. 236 00:14:51,220 --> 00:14:56,590 I'm going to call it I, and I'm just going to set it equal to the value of one. 237 00:14:56,590 --> 00:15:02,980 And then what I'm going to do is for my condition here, I'm going to check if I is less than ten. 238 00:15:02,980 --> 00:15:09,370 So as long as I is less than ten, we're going to execute this block of code here for our while loop. 239 00:15:09,370 --> 00:15:14,740 So I'm going to put I here and then to check if it's less than the value of ten. 240 00:15:14,770 --> 00:15:16,990 We use the less than symbol. 241 00:15:16,990 --> 00:15:18,700 And then we can put ten here. 242 00:15:19,210 --> 00:15:23,980 And we're going to do is we are going to increment I by one. 243 00:15:23,980 --> 00:15:27,250 So we're going to set I equal to itself. 244 00:15:27,250 --> 00:15:31,210 And then we're going to use the plus sign and add the value of one to I. 245 00:15:31,240 --> 00:15:34,060 So if I is one that means one plus one is two. 246 00:15:34,090 --> 00:15:38,320 We update the value in here to become two, and it's going to continually loop and do that. 247 00:15:38,320 --> 00:15:45,070 And then once the value of I surpasses ten or it's no longer less than ten, then this while loop is 248 00:15:45,070 --> 00:15:48,280 going to end because the condition has become false. 249 00:15:48,400 --> 00:15:52,480 So what I'm going to do here is I'm going to get rid of this yield statement, but instead I'm going 250 00:15:52,480 --> 00:15:54,370 to print out I into the console. 251 00:15:54,460 --> 00:15:56,650 And then we can go ahead and run our game. 252 00:15:58,800 --> 00:16:05,910 As you can see, I got printed as two and then three and then four, five, six, seven, eight, nine 253 00:16:05,910 --> 00:16:06,990 until it hit ten. 254 00:16:06,990 --> 00:16:12,210 And then once I has reached the value of ten, ten is not less than ten. 255 00:16:12,210 --> 00:16:16,170 So the condition became false and we broke out of that while loop. 256 00:16:16,620 --> 00:16:20,740 So as you can see, this while loop is acting the exact same as a for loop. 257 00:16:20,760 --> 00:16:23,400 It's just a little bit more complicated to do it this way. 258 00:16:23,400 --> 00:16:26,760 So that's why you would want to instead use a for loop. 259 00:16:27,420 --> 00:16:30,750 Okay, so the last loop we're going to take a look at is the repeat loop. 260 00:16:30,780 --> 00:16:33,960 This loop again is very similar to the while loop. 261 00:16:33,960 --> 00:16:35,750 But there is a slight difference. 262 00:16:35,760 --> 00:16:38,010 Before our while loop executes. 263 00:16:38,010 --> 00:16:38,310 Right. 264 00:16:38,310 --> 00:16:40,760 It first checks this condition up here. 265 00:16:40,770 --> 00:16:47,070 If this condition is already false, then this block of code is never going to have the chance to execute. 266 00:16:47,100 --> 00:16:52,800 However, with the repeat loop, instead of the condition being checked first, the condition is actually 267 00:16:52,800 --> 00:16:54,030 checked last. 268 00:16:54,030 --> 00:17:01,440 So that means if the condition is false at the start, it's still guaranteed to execute this loop at 269 00:17:01,440 --> 00:17:02,190 least once. 270 00:17:02,190 --> 00:17:03,300 So you'll see what I mean. 271 00:17:03,330 --> 00:17:07,920 To create a repeat loop, we type out the repeat keyword and then we hit enter. 272 00:17:07,920 --> 00:17:11,370 And then this next keyword gets automatically filled in here. 273 00:17:11,370 --> 00:17:13,020 And this is the until keyword. 274 00:17:13,020 --> 00:17:17,030 And after this until keyword is where we want to pass a condition. 275 00:17:17,040 --> 00:17:23,880 So as an example what I'm going to do is I'm going to set my I variable equal to we'll do zero. 276 00:17:24,500 --> 00:17:30,500 And inside of here we can print out I into the console and then we're going to increment I by one. 277 00:17:30,500 --> 00:17:33,560 So we do I is equal to I plus one. 278 00:17:33,950 --> 00:17:39,730 And then we don't want to continue looping once I becomes greater than ten. 279 00:17:39,740 --> 00:17:42,230 So this is reading out like basic English. 280 00:17:42,230 --> 00:17:47,900 Repeat this block of code until I becomes greater than the value of ten. 281 00:17:48,200 --> 00:17:51,020 So if we go ahead and hit run here. 282 00:17:52,630 --> 00:17:56,180 As you can see, we get ten iterations of our repeat loop. 283 00:17:56,200 --> 00:17:59,500 We print zero, one, two, three all the way to ten. 284 00:17:59,500 --> 00:18:03,940 And then once I has attained the value of 11, that is greater than ten. 285 00:18:03,940 --> 00:18:06,370 So our repeat loop has ended. 286 00:18:06,550 --> 00:18:10,330 Now I want to show you what happens if I is already 11. 287 00:18:10,330 --> 00:18:10,780 Right? 288 00:18:10,810 --> 00:18:17,140 If I is already 11, that means this condition has already evaluated to true and it will break out of 289 00:18:17,140 --> 00:18:17,800 this repeat loop. 290 00:18:17,800 --> 00:18:19,680 But that's not what's going to happen here. 291 00:18:19,690 --> 00:18:22,480 It's because we check this condition at the end. 292 00:18:22,480 --> 00:18:27,630 Meaning this block of code right here is guaranteed to execute at least once. 293 00:18:27,640 --> 00:18:30,250 So that means if we hit run here. 294 00:18:31,480 --> 00:18:35,680 As you can see, we still got it printed out inside of the console value of 11, right? 295 00:18:37,560 --> 00:18:43,440 We printed out 11 here, and then we incremented 11 by one, and then it finally broke out of the repeat 296 00:18:43,440 --> 00:18:43,870 loop. 297 00:18:43,890 --> 00:18:47,160 So actually what I'm going to do is I'm going to move this here. 298 00:18:47,930 --> 00:18:50,030 To be before our print statement. 299 00:18:50,830 --> 00:18:52,690 And then I'm going to run the game again. 300 00:18:53,620 --> 00:18:59,140 And as you can see, the value of 12 got printed out into the console, even though 12 is already greater 301 00:18:59,140 --> 00:19:00,460 than ten, right? 302 00:19:00,490 --> 00:19:03,070 It's because the condition is checked last. 303 00:19:03,100 --> 00:19:07,690 A repeat loop is always guaranteed to execute at least once. 304 00:19:08,660 --> 00:19:09,170 All righty. 305 00:19:09,170 --> 00:19:12,470 And that concludes this lecture on loops in Lua. 306 00:19:12,590 --> 00:19:17,420 I hope you've learned quite a lot and are ready to start putting these loops to use, because you will 307 00:19:17,420 --> 00:19:19,850 be using them a lot in your scripts. 308 00:19:19,850 --> 00:19:23,210 So get comfortable and I will see you in the next lecture.